package se.webbzon.boltzmann.editor.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.vecmath.AxisAngle4d;

import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public abstract class WorldStaticObjectPropertyFrame extends WorldObjectPropertyFrame {
	
	// The input fields for this property frame
	private final JTextField 	xInput, yInput, zInput,
								widthInput, heightInput, depthInput,
								exInput, eyInput, ezInput, angleInput;
	
	// The input check box for this property frame
	private final JCheckBox		visible, solid;
	
	/*============================================================
	Constructors
	============================================================*/

	public WorldStaticObjectPropertyFrame(EditorModel model, WorldStaticObject instance) {
		super(model, instance);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
		
		// The position panel
		JPanel positionPanel = new JPanel();
		positionPanel.setBorder(BorderFactory.createTitledBorder("Position"));
		positionPanel.setLayout(new GridLayout(2,3));
		positionPanel.add(new JLabel("x:"));
		positionPanel.add(new JLabel("y:"));
		positionPanel.add(new JLabel("z:"));
		xInput = new JTextField("" + instance.getX());
		yInput = new JTextField("" + instance.getY());
		zInput = new JTextField("" + instance.getZ());
		positionPanel.add(xInput);
		positionPanel.add(yInput);
		positionPanel.add(zInput);
		panel.add(positionPanel);
		
		// The size panel
		JPanel sizePanel = new JPanel();
		sizePanel.setBorder(BorderFactory.createTitledBorder("Size"));
		sizePanel.setLayout(new GridLayout(2,3));
		sizePanel.add(new JLabel("width:"));
		sizePanel.add(new JLabel("height:"));
		sizePanel.add(new JLabel("depth:"));
		widthInput = new JTextField("" + instance.getWidth());
		heightInput = new JTextField("" + instance.getHeight());
		depthInput = new JTextField("" + instance.getDepth());
		sizePanel.add(widthInput);
		sizePanel.add(heightInput);
		sizePanel.add(depthInput);
		panel.add(sizePanel);
				
		// The position panel
		JPanel rotationPanel = new JPanel();
		rotationPanel.setBorder(BorderFactory.createTitledBorder("Rotation (Axis-angle representation)"));
		rotationPanel.setLayout(new GridLayout(2,4));
		AxisAngle4d rotation = instance.getRotation();
		rotationPanel.add(new JLabel("ex:"));
		rotationPanel.add(new JLabel("ey:"));
		rotationPanel.add(new JLabel("ez:"));
		rotationPanel.add(new JLabel("rotation (radians):"));
		exInput = new JTextField("" + rotation.getX());
		eyInput = new JTextField("" + rotation.getY());
		ezInput = new JTextField("" + rotation.getZ());
		angleInput = new JTextField("" + rotation.getAngle());
		rotationPanel.add(exInput);
		rotationPanel.add(eyInput);
		rotationPanel.add(ezInput);
		rotationPanel.add(angleInput);
		panel.add(rotationPanel);
		
		// Visibility and solidity panel
		JPanel vsPanel = new JPanel();
		vsPanel.setBorder(BorderFactory.createTitledBorder("Visibility and Solidity:"));
		vsPanel.setLayout(new BoxLayout(vsPanel,BoxLayout.Y_AXIS));
		visible = new JCheckBox("Visible"); 
		visible.setSelected(instance.isVisible());
		solid = new JCheckBox("Solid");
		solid.setSelected(instance.isSolid());
		vsPanel.add(visible);
		vsPanel.add(solid);
		panel.add(vsPanel);
		
		addPanel("Layout", panel);
	}
	
	/*============================================================
	Constructors
	============================================================*/

	@Override public boolean apply(Collection<String> errorMessages) {
		WorldStaticObject instance = (WorldStaticObject) getInstance();
		try {
			final double x = Double.parseDouble(xInput.getText());
			final double y = Double.parseDouble(yInput.getText());
			final double z = Double.parseDouble(zInput.getText());
			final double width = Double.parseDouble(widthInput.getText());
			final double height = Double.parseDouble(heightInput.getText());
			final double depth = Double.parseDouble(depthInput.getText());
			final double ex = Double.parseDouble(exInput.getText());
			final double ey = Double.parseDouble(eyInput.getText());
			final double ez = Double.parseDouble(ezInput.getText());
			final double angle = Double.parseDouble(angleInput.getText());
			instance.setPosition(x, y, z);
			instance.setSize(width, height, depth);
			instance.setRotation(ex, ey, ez, angle);
			instance.setVisible(visible.isSelected());
			instance.setSolid(solid.isSelected());
			getModel().changeInstance(this, instance);
			return true;
		} catch (NumberFormatException e) {
			errorMessages.add(e.getMessage());
			return false;
		}
		
	}

}
